﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using LeagueOfRila.Model;

namespace LeagueOfRila.Controller
{
    public class MenuConnectionController : BaseController
    {
        private KeyboardState previousState;
        

        public override string viewName
        {
            get { return "LeagueOfRila.View.MenuConnectionView"; }
            set {}
        }
        public override string modelName
        {
            get { return "LeagueOfRila.Model.MenuConnectionModel"; }
            set { }
        }
        public MenuConnectionController()
        {
        }

        public MenuConnectionModel GetModel()
        {
            return this.model as MenuConnectionModel;
        }
        public override void Update(GameTime gameTime)
        {
            KeyboardState currentState = Keyboard.GetState();

            if (previousState == null)
                previousState = currentState;

            base.Update(gameTime);
            //if (Keyboard.GetState().IsKeyDown(Keys.A))
            //    this.GetModel().pseudo += "A";
            
            List<Keys> listprevious = previousState.GetPressedKeys().ToList();
            foreach(Keys key in listprevious)
            {
                if (!currentState.GetPressedKeys().Contains(key))
                {
                    if (key.ToString().ToLower() == "enter")
                    {
                        Client.PlaySound(Client.entersound);
                        // Affiche le menu de choix de personnages
                        if (this.GetModel().VerifPseudo(this.GetModel().player.pseudo))
                        {
                            Client.player = this.GetModel().player;
                            Client.GotoNextStep("MenuCharacterChoice");
                        }
                        else
                            this.GetModel().DisplayError();

                        
                    }
                    else
                    {
                        this.GetModel().UpdatePseudo(key.ToString().ToLower());

                        Client.PlaySound(Client.keysound);
                    }
                        
                }
            }

            previousState = currentState;

        }
    }
}
